import logging

from selenium.webdriver.common.by import By

from pageobj import BasePage
from settings import host

logger = logging.getLogger(__name__)


class CartPage(BasePage):
    """商品购物车页面"""

    url = host + '/checkout/cart'

    # "继续支付"按钮
    checkoutBtn = (By.CSS_SELECTOR, '.btn-checkout')

    # 货运地址
    Firstname = (By.ID, 'billing:firstname')
    Lastname = (By.ID, 'billing:lastname')
    Email = (By.ID, 'billing:email')
    Telephone = (By.ID, 'billing:telephone')
    StreetAddress1 = (By.ID, 'billing:street1')
    StreetAddress2 = (By.ID, 'billing:street2')
    Country = (By.ID, 'billing:country')
    State = (By.ID, 'address:state')
    City = (By.ID, 'billing:city')
    ZipCode = (By.ID, 'billing:zip')

    # 货运地址下拉选择框
    AddressList = (By.CSS_SELECTOR, '.address_list')

    # 买家留言
    OrderRemark = (By.CSS_SELECTOR, '.order_remark')

    # 支付订单
    CheckoutBtn = (By.ID, 'onestepcheckout-place-order')

    def cart2pay(self):
        """购物车-->支付

        :return:
        """
        logger.info('开始下单支付')
        logger.debug('1.点击"继续支付"')
        self.click(self.checkoutBtn)
        logger.debug('2.等待跳转支付页面')
        self.url_changes(self.url)

        try:
            self.findElement(self.AddressList)
        except:
            logger.debug('3.首次下单需要填写货运地址')
            self.input(self.Telephone, '18709247051')
            self.input(self.StreetAddress1, '秦都区世纪大道')
            self.input(self.StreetAddress2, '上林路街道')
            self.select_by_value(self.Country, 'CN')
            self.select_by_value(self.State, 'SNX')
            self.input(self.City, '西安')
            self.input(self.ZipCode, '710000')
        else:
            logger.debug('3.已有货运地址，使用默认收货地址')

        logger.debug('4.输入买家留言')
        self.input(self.OrderRemark, '请安排周六日配送取货')

        logger.debug('5.点击"支付订单"')
        self.click(self.CheckoutBtn)

        logger.debug('6.等待页面跳转')
        self.url_changes(self.url)
